<template>
  <div class="container">
    <van-nav-bar
      title="酒店"
      left-text="返回"
      left-arrow
      @click-left="onClickLeft"
    >
      <template #right>
        <van-image
          round
          width="1rem"
          height="1rem"
          fit="contain"
          :src="this.userInfo.photo"
        />
      </template>
    </van-nav-bar>

    <ul v-for="(value, index) of hotelList" :key="index">
      <li>
        <div class="showImg">
          <img :src="value.imgsrc" alt="" />
        </div>
        <div class="showText">
          <h1 class="title">{{ value.title }}</h1>
          <span>
            <img
              @click.stop="changecollectImgSrc(value)"
              class="collect"
              style="width: 0.5rem; float: right"
              :src="value.collectImgSrc"
              alt=""
          /></span>
          <p class="evaluate">{{ value.evaluate }}</p>
          <p class="address">{{ value.address }}</p>
        </div>
      </li>
    </ul>
  </div>
</template>
<script>
import axios from "axios";
export default {
  data() {
    return {
      userInfo: JSON.parse(localStorage.getItem("userInfo")),
      hotelList: [],
      myCollects: JSON.parse(localStorage.getItem("myCollects")) || [],
    };
  },
  created() {
    /* console.log(this.userInfo); */
    this.loadAddressList();
  },
  computed: {},
  methods: {
    changecollectImgSrc(value) {
      /* console.log(value.collectImgSrc); */
      if (
        value.collectImgSrc == "http://qzek6s6bf.hn-bkt.clouddn.com/aixin0.png"
      ) {
        value.collectImgSrc = "http://qzek6s6bf.hn-bkt.clouddn.com/aixin1.png";

        this.nowDate();
        /* console.log(value.title); */
        this.myCollects.unshift({
          title: value.title,
          imgsrc: value.imgsrc,
          nowTime: this.nowTime,
        });

        /*   console.log(this.myCollects); */
        localStorage.setItem("myCollects", JSON.stringify(this.myCollects));
      } else {
        value.collectImgSrc = "http://qzek6s6bf.hn-bkt.clouddn.com/aixin0.png";
        this.myCollects.shift();

        /*   console.log(this.myCollects); */
        localStorage.setItem("myCollects", JSON.stringify(this.myCollects));
      }
    },

    /* 获取当前时间方法 */
    nowDate() {
      const nowDate = new Date();
      const date = {
        year: nowDate.getFullYear(),
        month: nowDate.getMonth() + 1,
        date: nowDate.getDate(),
        hours: nowDate.getHours(),
        Minutes: nowDate.getMinutes(),
      };
      const newmonth = date.month > 10 ? date.month : "0" + date.month;
      const day = date.date > 10 ? date.date : "0" + date.date;
      const Minute =
        nowDate.getMinutes() < 10
          ? "0" + nowDate.getMinutes()
          : nowDate.getMinutes();
      this.nowTime =
        date.year +
        "-" +
        newmonth +
        "-" +
        day +
        " " +
        date.hours +
        ":" +
        Minute;
    },
    onClickLeft() {
      this.$router.go(-1);
    },
    loadAddressList() {
      axios.get("/api/hotelList").then((res) => {
        /*        console.log(res.data.data.addressList); */
        const result = res.data.data.hotelList;
        this.myCollects.map((item) => {
         /*  console.log(item.title); */
          result.map((value) => {
       /*      console.log(value); */
            if (item.title == value.title) {
              value.collectImgSrc =
                "http://qzek6s6bf.hn-bkt.clouddn.com/aixin1.png";
            }
          });
        });
        this.hotelList = result;
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.container {
  margin: 0 auto;
  width: 96%;
  height: 15rem;
  margin-top: 3%;
  background-color: rgb(247, 247, 247);
}
li {
  margin-left: 2%;
  margin-top: 4%;
  width: 96%;
  background-color: rgb(248, 248, 248);
  border-radius: 4%;
  box-shadow: 1px 1px 3px gray;
}
img {
  width: 100%;
}
h1 {
  font-size: 18px;
}
ul {
  position: relative;
  margin-top: 3%;
  width: 100%;
  height: 2.7rem;
  border-radius: 3%;
  background-color: rgb(255, 255, 255);
}
.showImg {
  position: absolute;
  width: 50%;
  height: 100%;
}
.showImg img {
  width: 96%;
  margin-left: 2%;
  margin-top: 5%;
}
.showText {
  position: absolute;
  left: 50%;
  width: 50%;
  height: 100%;
}
.title {
  margin-top: 15%;
  font-size: 18px;
}
.evaluate {
  margin-top: 10%;
  margin-left: 5%;
  font-size: 12px;
}
.address {
  margin-top: 10%;
  margin-left: 5%;
  font-size: 14px;
}
h1 {
  font-size: 18px;
  padding-top: 3%;
  margin-left: 4%;
}
</style>
